package com.study.compose.beta.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.module.compose.base.BaseActivity

/**
 * 作者：zhangjunfei 2021/6/15 5:43 下午
 * 主页：https://gitee.com/ZhangQQ_123/projects
 * 描述：compose路由
 */
class NavActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NavSample()
        }
    }
}

@Composable
private fun NavSample() {

    val navController = rememberNavController()

    NavHost(navController = navController, startDestination = "firstPage") {
        composable("firstPage") {
            FirstPage(navController = navController)
        }
        composable("secondPage") {
            SecondSample(navController = navController)
        }
    }
}

@Composable
private fun FirstPage(navController: NavController) {
    DisposableEffect(key1 = "SecondSample") {

        Log.e("生命周期", "页面开始")
        onDispose {
            Log.e("生命周期", "页面销毁")
        }
    }

    Column(
        Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Button(onClick = {
            navController.navigate("secondPage")
        }) {
            Text(text = "页面跳转")
        }
    }

}

@Composable
private fun SecondSample(navController: NavController) {
    Column(
        Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Button(onClick = {
            navController.popBackStack()
        }) {
            Text(text = "页面返回")
        }
        Spacer(modifier = Modifier.height(20.dp))
        Text(text = "新的页面")
    }
}

